#! encoding=utf-8

import requests
from common import HOST
import traceback

def add(tag):
    url = HOST + '/group'
    print "POST /user 创建用户组 ..."
    ret = None
    try:
        ret = requests.session().post(url, {'tag': tag}).content
    except:
        print traceback.format_exc()
    finally:
        print ret


def get(skip, limit):
    url = HOST + '/group'
    print "GET /group 获取所有用户组 ..."
    ret = None
    try:
        ret = requests.session().get(url + '?skip=' + skip + '&limit=' + limit).content
    except:
        print traceback.format_exc()
    finally:
        print ret


def delete(group_id):
    url = HOST + '/group/' + group_id
    print "Delete /group/{group_id} 删除某个用户组 ..."
    try:
        requests.session().delete(url)
    except:
        print traceback.format_exc()


def getGroup(group_id):
    url = HOST + '/group/' + group_id
    print "GET /group/{group_id} 获取单个用户组 ..."
    ret = None
    try:
        ret = requests.session().get(url).content
    except:
        print traceback.format_exc()
    finally:
        print ret


def update(group_id, tag):
    url = HOST + '/group/' + group_id
    print "PUT /group/{group_id} 修改单个用户组 ..."
    ret = None
    try:
        ret = requests.session().put(url, {'tag': tag}).content
    except:
        print traceback.format_exc()
    finally:
        print ret


def user(group_id, user_id):
    url = HOST + '/group/' + group_id + '/' + user_id
    print "POST /group/{group_id}/{user_id} 向用户组中加入用户 ..."
    ret = None
    try:
        ret = requests.session().post(url).content
    except:
        print traceback.format_exc()
    finally:
        print ret


def deleteUser(group_id, user_id):
    url = HOST + '/group/' + group_id + '/' + user_id
    print "DELETE /group/{group_id}/{user_id} 删除用户组中的用户 ..."
    try:
        requests.session().delete(url)
    except:
        print traceback.format_exc()


def query(query, skip, limit):
    url = HOST + '/group_query/' + '?query=' + query + '&skip=' + skip + '&limit=' + limit
    print "GET /group_query 查询用户组 ..."
    ret = None
    try:
        ret = requests.session().get(url).content
    except:
        print traceback.format_exc()
    finally:
        print ret



